﻿using Dapper;
using Domain.Entity.System_numbering;
using Domain.IRepository.System_numbering;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.DependencyInjection;

namespace EFCore.Repository.System_numbering
{
    [Dependency(ServiceLifetime.Scoped)]
    public class BHRepository : IBHRepository
    {
        private readonly MyDbContext myDbContext;

        //public MyDbContext myDbContext { get; set; }
        public BHRepository(MyDbContext myDbContext) 
        {
            this.myDbContext = myDbContext;
        }
        public string BHGZ(string prefix)
        {
            StringBuilder sb = new StringBuilder();
            using (SqlConnection conn = new SqlConnection(myDbContext._connectionString))
            {
                string sql = $"select * from [dbo].[BH] where [En_prefix]=@prefix";
                var parameters = new DynamicParameters();
                parameters.Add("prefix", prefix);
                List<BH> bHs = conn.Query<BH>(sql, parameters).ToList();
                foreach (var item in bHs)
                {
                    string dataRu = item.En_time;
                    //string dataRu = en_time;
                    //switch
                    //{
                    //    "1" => "YYYY",
                    //    "2" => "YYYYMM",
                    //    "3" => "YYYYMMDD",
                    //    _ => throw new ArgumentOutOfRangeException()
                    //};
                    string time = DateTime.Now.ToString(dataRu);
                    string time2 = item.Last_data.ToString(dataRu);
                    int num = (int)item.Max_num;
                    if (time != time2)
                    {
                        num = 0;
                    }
                    num++;
                    string s = num.ToString().PadLeft(3, '0');
                    sb.Append(item.En_prefix);
                    sb.Append(time);
                    sb.Append(s);
                }
                return sb.ToString();
            }
        }

        public int GetBH(string prefix)
        {
            StringBuilder sb = new StringBuilder();
            int num2 = 0;
            using (SqlConnection conn = new SqlConnection(myDbContext._connectionString))
            {
                string sql = $"select * from [dbo].[BH] where [En_prefix]=@prefix";
                var parameters = new DynamicParameters();
                parameters.Add("prefix", prefix);
                List<BH> bHs = conn.Query<BH>(sql, parameters).ToList();
                foreach (var item in bHs)
                {
                    string dataRu = item.En_time;
                    //string dataRu = en_time;
                    //switch
                    //{
                    //    "1" => "YYYY",
                    //    "2" => "YYYYMM",
                    //    "3" => "YYYYMMDD",
                    //    _ => throw new ArgumentOutOfRangeException()
                    //};
                    string time = DateTime.Now.ToString(dataRu);
                    string time2 = item.Last_data.ToString(dataRu);
                    int num = (int)item.Max_num;
                    if (time != time2)
                    {
                        num = 0;
                    }
                    num++;
                    string s = num.ToString().PadLeft(3, '0');
                    string sql2 = $"update BH set Max_num=@Max_num,Last_data=@Last_data where En_code=@En_code";
                    num2 = conn.Execute(sql2, new DynamicParameters(new { Max_num = num, Last_data = DateTime.Now, en_code = item.En_code }));
                }
              
                return num2;
            } 
        }
    }
}
